/**
 * Created by jianglinjie on Thu Sep 21 2017
 * @flow
 */
'use strict';

import mongoose from 'mongoose';
import type { UserInfo } from '../../types/UserInfo';
const Schema = mongoose.Schema;

const userSchema = new Schema({
  registerType: {
    type: String,
    required: true,    
  },
  userName: { 
    type: String, 
    index: {
      unique: true,
      sparse: true
    }
  },
  phone: { 
    type: String, 
    index: {
      unique: true,
      sparse: true
    } 
  },
  email: { 
    type: String, 
    index: {
      unique: true,
      sparse: true
    } 
  },
  pwd: { 
    required: true,    
    type: String 
  },
  encryptPwd: { 
    required: true,    
    type: String 
  }, 
  pwdModifyTime: {
    type: Date, 
    default: Date.now     
  },
  gender: { 
    type: String,
    default: '',
  },
  avatar: { 
    type: String,
    default: '',    
  },
  nickName: { 
    type: String,
    default: '',    
  },
  createTime: { 
    type: Date, 
    default: Date.now 
  },  
});

userSchema.methods.toUserInfo = function(): UserInfo {
  return {
    registerType: this.registerType,
    uid: this.id,
    userName: this.userName || '',
    phone: this.phone || '',
    encryptPwd: this.encryptPwd || '',    
    email: this.email || '',
    gender: this.gender,
    avatar: this.avatar,
    nickName: this.nickName,
  }
};

export default mongoose.model('User', userSchema);